package com.lith.datalog.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lith.datalog.annotation.DataLog;
import com.lith.datalog.entity.User;
import com.lith.datalog.mapper.UserMapper;
import com.lith.datalog.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * UserController
 * </p>
 *
 * @author Tophua
 * @since 2020/5/7
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    private final UserMapper userMapper;

    @GetMapping("{id}")
    public User getById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @DataLog
    @PostMapping
    public Boolean save(@RequestBody User user) {
        return userService.save(user);
    }

    @DataLog
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@RequestBody User user) {
        User nUser = new User();
        nUser.setId(2);
        nUser.setName("代码更新");
        nUser.updateById();
        userService.update(Wrappers.<User>lambdaUpdate()
                .set(User::getName, "批量")
                .in(User::getId, 3, 4));
        userMapper.updateTest();
        return userService.updateById(user);
    }

    @DataLog
    @DeleteMapping("{id}")
    public Boolean removeById(@PathVariable Integer id) {
        return userService.removeById(id);
    }
}
